﻿Imports System
Imports System.Data
Imports System.String
Imports System.Text
Imports System.Collections
''' <summary>
''' 銷售訂單發票
''' </summary>
''' <remarks> songrenhua 2008-09-27 </remarks>
Public Class Frm_SALSCF

    Implements IDServer.IDServer.IDServer
    Private MasterDS As DataSet
    Private DetailDS As DataSet
    Private WithEvents MainForm As Frm_MainForm


#Region "Property"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region

    Public FindBool As Boolean = True

#Region "interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

#End Region

    ''' <summary>
    ''' 填充店鋪名稱
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ShopNoItem()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select distinct fShortName from BSC_StoreHouse  "
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CbShopNo.DataSource = Dset.Tables(0)
            Me.CbShopNo.DisplayMember = "fShortName"
            Me.CbShopNo.Text = ""
        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub

    ''' <summary>
    ''' 填充單據狀態
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OrdeModeItem()
        Try
            Dim Dset As New DataSet
            Dim sql As String = ""
            sql &= "select distinct ListA from BSC_OtherDefineB where  "
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CbOrdeMode.DataSource = Dset.Tables(0)
            Me.CbOrdeMode.DisplayMember = "ListA"


        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub



#Region "function or sub"


    Private Sub BindMasterGrid()
        Dim mTable As New DataTable()
        Try
            With mTable
                .Columns.Add("fSaleOrdeNO")
                .Columns.Add("fWriteNo")
                .Columns.Add("ShopNo")
                .Columns.Add("fSaleDate")
                .Columns.Add("fOperaterMan")
                .Columns.Add("fOperateDate")
                .Columns.Add("fCheckMan")
                .Columns.Add("fCheckDate")
                .Columns.Add("fOrdeStatus")
                .Columns.Add("fDSIA")
                .Columns.Add("fDSIB")
                .Columns.Add("fBSI")
                .Columns.Add("FTAmount")
                .Columns.Add("fPayMode")
                .Columns.Add("fRemark")
                .Columns.Add("fBuiltDate")
                .Columns.Add("fUpdateDate")
                .Columns.Add("fAreaID")
                .Columns.Add("fDeliverMode")
                .Columns.Add("fDeliverDate")
                .Columns.Add("fCustomerName")
                .Columns.Add("fLinkMan")
                .Columns.Add("fLinkPhone")
                .Columns.Add("fDeliverAddress")
                .Columns.Add("fEmployeeID")
                .Columns.Add("fOperateComputer")


            End With
            MasterDS = New DataSet()
            MasterDS.Tables.Add(mTable)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(0).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
        Catch ex As Exception
            MsgBox("BindMasterGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try

    End Sub

    Private Sub BindDetailGrid()


        Dim mTable As New DataTable
        Try
            With mTable
                .Columns.Add("FNum")
                .Columns.Add("FDate")
                .Columns.Add("ShopNo")
                .Columns.Add("FSaleOrdeNo")
                .Columns.Add("FDeliverId")
                .Columns.Add("FWriteNo")
                .Columns.Add("FStore")
                .Columns.Add("FType")
                .Columns.Add("FNeedDate")
                .Columns.Add("FTodayBring")
                .Columns.Add("FPayInFull")
                .Columns.Add("FRemark")
                .Columns.Add("FDSIA")
                .Columns.Add("FDSIB")
                .Columns.Add("FBSI")
                .Columns.Add("FTAmount")
                .Columns.Add("FReceipt")
                .Columns.Add("FOther")
                .Columns.Add("BillstatusID")
                .Columns.Add("BulidDate")
                .Columns.Add("BulidBy")
                .Columns.Add("Checkby")
                .Columns.Add("CheckDate")
                .Columns.Add("CheckComputer")




            End With
            DetailDS = New DataSet
            DetailDS.Tables.Add(mTable)
            Me.DataGridView2.DataSource = DetailDS.Tables(0)

            With Me.DataGridView2
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(1).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With

        Catch ex As Exception
            MsgBox("BindDetailGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try


    End Sub

    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable
        Dim mHash As New Hashtable
        Try

            With mHash
                Select Case Index
                    Case 0
                        .Add("fSaleOrdeNO", "銷售單號")
                        .Add("fWriteNo", "手寫單號")
                        .Add("ShopNo", "門市")
                        .Add("fSaleDate", "銷售日期")
                        .Add("fOperaterMan", "操作員")
                        .Add("fOperateDate", "操作日期")
                        .Add("fCheckMan", "核對人員")
                        .Add("fCheckDate", "核對日期")
                        .Add("fOrdeStatus", "訂單狀態")
                        .Add("fDSIA", "實收訂金")
                        .Add("fDSIB", "應付訂金")
                        .Add("fBSI", "尾數")
                        .Add("FTAmount", "總金額")
                        .Add("fPayMode", "付款類型")
                        .Add("fRemark", "備注")
                        .Add("fBuiltDate", "創建日期")
                        .Add("fUpdateDate", "修改日期")
                        .Add("fAreaID", "客戶區域")
                        .Add("fDeliverMode", "送貨模式")
                        .Add("fDeliverDate", "送貨日期")
                        .Add("fCustomerName", "顧客姓名")
                        .Add("fLinkMan", "聯系人")
                        .Add("fLinkPhone", "聯繫電話")
                        .Add("fDeliverAddress", "送貨地址")
                        .Add("fEmployeeID", "營業員")
                        .Add("fOperateComputer", "操作主機")



                    Case 1
                        .Add("FNum", "發票編號")
                        .Add("FDate", "發票日期")
                        .Add("ShopNo", "門市")
                        .Add("FSaleOrdeNo", "銷售單號")
                        .Add("FDeliverId", "送貨單號")
                        .Add("FWriteNo", "手寫單號")
                        .Add("FStore", "出貨倉庫")
                        .Add("FType", "發票類型")
                        .Add("FNeedDate", "到期日")
                        .Add("FTodayBring", "取日自取")
                        .Add("FPayInFull", "全部付清")
                        .Add("FRemark", "備注")
                        .Add("FDSIA", "實收訂金")
                        .Add("FDSIB", "應付訂金")
                        .Add("FBSI", "尾數")
                        .Add("FTAmount", "總金額")
                        .Add("FReceipt", "實收金額")
                        .Add("FOther", "其他")
                        .Add("BillstatusID", "單據狀態")
                        .Add("BulidDate", "建立日期")
                        .Add("BulidBy", "建立者")
                        .Add("Checkby", "審核者")
                        .Add("CheckDate", "核准日期")
                        .Add("CheckComputer", "核准電腦")

                End Select
            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing

        End Try
    End Function

#End Region

    ''' <summary>
    ''' 加載頁面
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Frm_SALSCF_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            Me.ShopNoItem()
            BindMasterGrid()
            BindDetailGrid()

        Catch ex As Exception
            MsgBox(My.Resources.LoadFormError, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        Try
            Dim sql As String = ""
            Dim WhereStr As String = ""

            If Me.TbSaleOrdeNo.Text.Length > 0 Then
                WhereStr += " and fSaleOrdeNo='" & Me.TbSaleOrdeNo.Text.Trim & "'"
            End If

            If Me.TbWriteNo.Text.Length > 0 Then
                WhereStr += " and fWriteNo='" & Me.TbWriteNo.Text.Trim & "'"
            End If

            If Me.CbShopNo.Text.Length > 0 Then
                WhereStr += "and b.fShortName ='" & Me.CbShopNo.Text.Trim & "'"
            End If

            If Me.CbOrdeMode.Text.Length > 0 Then
                WhereStr += "and c.listA = '" & Me.CbOrdeMode.Text.Trim & "'"
            End If


            If Me.DateTimePicker1.Value.ToString.Length > 0 And Me.DateTimePicker2.Value.ToString.Length <= 0 Then

            ElseIf Me.DateTimePicker1.Value.ToString.Length <= 0 And Me.DateTimePicker2.Value.ToString.Length >= 0 Then

            ElseIf Me.DateTimePicker1.Value.ToString.Length >= 0 And Me.DateTimePicker2.Value.ToString.Length >= 0 Then
                WhereStr += " and fSaleDate between '" & Format(Me.DateTimePicker1.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "' and '" & Format(Me.DateTimePicker2.Value.ToString("yyyy-MM-dd"), "yyyy-MM-dd") & "'"
            End If


            sql = "select fSaleOrdeNO,fWriteNo,b.fShortName as ShopNO,fSaleDate,fOperaterMan,fOperateDate,fCheckMan,fCheckDate," & vbCrLf
            sql &= " c.listA as fOrdeStatus ,fDSIA,fDSIB,fBSI,FTAmount,fPayMode,a.fRemark,fBuiltDate,fUpdateDate,fAreaID," & vbCrLf
            sql &= " d.listA as fDeliverMode,fDeliverDate,fCustomerName,fLinkMan,fLinkPhone,fDeliverAddress,a.fEmployeeID,fOperateComputer" & vbCrLf
            sql &= "  FROM Sal_SaleOrdeA  a left join BSC_StoreHouse b on a.ShopNo =b.fCode " & vbCrLf
            sql &= "inner join BSC_OtherDefineB c  on  a.fOrdeStatus = c.listcode " & vbCrLf
            sql &= "left join (select listcode, listA from BSC_OtherDefineB where ItemNo = '9' ) d on a.fDeliverMode = d.listcode " & vbCrLf
            sql &= " where  1=1 and c.itemno='16'" & vbCrLf
            sql &= WhereStr

            MasterDS = ReturnDataset(sql)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)
        Catch ex As Exception
            MsgBox("BtnFind_Click:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub DataGridView2_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick


    End Sub

    'Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    '    Dim sql As String = ""
    '    Dim fSaleOrdeNo As String = ""
    '    Try
    '        fSaleOrdeNo = Me.DataGridView1.CurrentRow.Cells("fSaleOrdeNo").Value
    '        sql = "select a.FNum,FDate,c.fShortName as ShopNo,a.FSaleOrdeNo,a.FDeliverId,A.FWriteNo,FStore,a.FType,a.FNeedDate,a.FTodayBring,a.FPayInFull,"
    '        sql += "a.FRemark,a.FDSIA,a.FDSIB,a.FBSI,A.FTAmount,A.FReceipt,A.FOther,d.listA as BillstatusID,A.BulidDate,BulidBy,Checkby,CheckDate,CheckComputer "
    '        sql += " from Sal_InvoiceA a left join Sal_SaleOrdeA b on a.fSaleOrdeNo = b.fSaleOrdeNo "
    '        sql += "left join BSC_StoreHouse c  on a.ShopNo =c.fCode   "
    '        sql += "inner join BSC_OtherDefineB d on a.BillstatusID = d.listcode "
    '        sql += " where d.itemno='16'and  a.fSaleOrdeNo='" & fSaleOrdeNo & "'"
    '        DetailDS = ReturnDataset(sql)

    '        Me.DataGridView2.DataSource = DetailDS.Tables(0)
    '    Catch ex As Exception
    '        MsgBox("DataGridView1_CellContentClick:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
    '    End Try
    'End Sub

    Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged


    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView2_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView2.RowPostPaint

        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim sql As String = ""
        Dim fSaleOrdeNo As String = ""
        Try
            fSaleOrdeNo = Me.DataGridView1.CurrentRow.Cells("fSaleOrdeNo").Value
            sql = "select a.FNum,FDate,c.fShortName as ShopNo,a.FSaleOrdeNo,a.FDeliverId,A.FWriteNo,FStore,a.FType,a.FNeedDate,a.FTodayBring,a.FPayInFull,"
            sql += "a.FRemark,a.FDSIA,a.FDSIB,a.FBSI,A.FTAmount,A.FReceipt,A.FOther,d.listA as BillstatusID,A.BulidDate,BulidBy,Checkby,CheckDate,CheckComputer "
            sql += " from Sal_InvoiceA a left join Sal_SaleOrdeA b on a.fSaleOrdeNo = b.fSaleOrdeNo "
            sql += "left join BSC_StoreHouse c  on a.ShopNo =c.fCode   "
            sql += "left join BSC_OtherDefineB d on a.BillstatusID = d.listcode"
            sql += " where d.itemno='16'and  a.fSaleOrdeNo='" & fSaleOrdeNo & "'"
            DetailDS = ReturnDataset(sql)

            Me.DataGridView2.DataSource = DetailDS.Tables(0)
        Catch ex As Exception
            MsgBox("DataGridView1_CellContentClick:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        FormatDgrd(DataGridView1, "fOrdeStatus")
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged

    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    End Sub

    Private Sub CbShopNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CbShopNo.SelectedIndexChanged

    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub
End Class
